package com.care.controller.weixin;

import com.alibaba.fastjson.JSONObject;
import com.care.author.AuthRequired;
import com.care.controller.vo.JsonResult;
import com.care.dao.mapper.CommunityMapper;
import com.care.dao.mapper.ServiceMapper;
import com.care.dao.mapper.UserMapper;
import com.care.dao.model.Service;
import com.care.dao.model.User;
import com.care.service.ServiceInterface;
import com.care.utils.page.PageView;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;

import jakarta.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 小程序端志愿者的接口
 */
@RestController
@RequestMapping("api/volunteer")
public class VolunteerApi extends ApiBaseController {

    @Autowired
    private ServiceMapper serviceMapper;

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private CommunityMapper communityMapper;

    /**
     * 查询我的服务列表
     * @param reqMap
     * @param request
     * @return
     */
    @RequestMapping("my/service/list")
    @AuthRequired
    public JsonResult shopList(@RequestBody Map<String, Object> reqMap, HttpServletRequest request) {
        PageView<Service> pageView = new PageView<>();
        User user = getCurrUser(request);
        pageView = pageView.startPage(reqMap);
        Example example = new Example(Service.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId", user.getUserId());
        example.orderBy("publishTime").desc();
        List<Service> list = serviceMapper.selectByExampleAndRowBounds(example, new RowBounds(pageView.getFirstResult(), pageView.getMaxresult()));
        // 切割图片
        for (Service item : list) {
            if(StringUtils.isNotBlank(item.getPics())) {
                String[] url = item.getPics().split(",");
                List<String> urls = new ArrayList<>();
                for (int i=0; i<url.length; i++) {
                    urls.add(url[i]);
                }
                item.setPicList(urls);
            }
        }
        pageView.setRecords(list);
        pageView.setTotalrecord(serviceMapper.selectCountByExample(example));//总记录数
        return JsonResult.success(pageView);
    }

    /**
     * 服务详情
     * @param sid
     * @return
     */
    @RequestMapping("service/detail")
    public JsonResult detail(@RequestParam Integer sid) {
        JsonResult result = new JsonResult(true, "0", "查询成功");
        Service service = serviceMapper.selectByPrimaryKey(sid);
        // 切割图片、标签
        if(StringUtils.isNotBlank(service.getPics())) {
            String[] url = service.getPics().split(",");
            List<String> urls = new ArrayList<>();
            for (int i=0; i<url.length; i++) {
                urls.add(url[i]);
            }
            service.setPicList(urls);
        }
        result.setData(service);
        return result;
    }

    /**
     * 发布服务请求
     * @param req
     * @param request
     * @return
     */
    @RequestMapping("save")
    @AuthRequired
    public JsonResult saveService(@RequestBody JSONObject req, HttpServletRequest request){
        User user = getCurrUser(request);
        Service service = JSONObject.parseObject(req.toJSONString(), Service.class);
        service.setUserId(user.getUserId());
        service.setPublishTime(new Date());
        serviceMapper.insertSelective(service);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(service);
        return jsonResult;
    }

    /**
     * 服务反馈
     * @param req
     * @param request
     * @return
     */
    @RequestMapping("feedback")
    @AuthRequired
    public JsonResult feedbackService(@RequestBody JSONObject req, HttpServletRequest request){
        Integer serviceId = req.getInteger("serviceId");//
        Service service = serviceMapper.selectByPrimaryKey(serviceId);
        service.setFeedbackRate(req.getInteger("rate"));//反馈评分
        service.setFeedback(req.getString("feedback"));//反馈内容
        service.setStatus(ServiceInterface.SERVICE_STATUS_FEEDBACK);//完成反馈状态
        serviceMapper.updateByPrimaryKeySelective(service);//更新数据库
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(service);
        return jsonResult;
    }
}
